<?php
class App_Prize
{
    /**
     * 获取正在执行及未执行完的福利列表
     */
    public static function get_list($uid){
        //首先获取这个用户的等级
        $level_id = DB::select('userLevelId')
            ->from('user_v2')
            ->where('uid', '=', $uid)
            ->execute()
            ->get('userLevelId');
    
        $prize_ids = DB::select('prizes')
                ->from('huiyuan_level')
                ->where('id', '=', $level_id)
                ->execute()
                ->get('prizes');

        $prize_ids = trim($prize_ids, ',');
        $arr_prizes = explode(',', $prize_ids);
        
        //查询这个用户已经进行的福利
        $prizes = DB::select()
            ->from('huiyuan_prize')
            ->where('id', 'in', $arr_prizes)
            ->execute()
            ->as_array();
        
        $result = array();
        foreach($prizes as $prize)
        {
            
            $record = DB::select()
                ->from('huiyuan_prize_record')
                ->where('pid', '=', $prize['id'])
                ->where('uid', '=', $uid)
                ->execute()
                ->current();
            
            if(empty($record))
            {
                $prize['record'] = NULL;
                $result[] = $prize;
            }else{
                $prize['record'] = $record;
                $result[] = $prize;
            }
        }
        
        return $result;
        
    }
    
    //
    public static function get_finish_list($uid)
    {
        $query = DB::select()
                ->from(array('huiyuan_prize_record' , 'r'))
                ->join(array('huiyuan_prize' , 'p'), 'LEFT')
                ->on('r.pid', '=', 'p.id')
                ->where('r.uid', '=', $uid)
                ->where('r.status', '=', '1')
                ->execute()
                ->as_array();
        return $query;
    }
    
    // 更新进度
    public static function update_progress($uid, $pid)
    {
        //首先查询这个福利下面有几个任务
        $task_ids = DB::select('tasks')
                ->from('huiyuan_prize')
                ->where('id', '=', $pid)
                ->execute()
                ->get('tasks');
        $task_ids = trim($task_ids, ',');
        $arr_tasks = explode(',', $task_ids);
        $count_total = count($arr_tasks);
        
        //再次查询这个用户在这个福利下完成了几个任务
        $records = DB::select()
                ->from('huiyuan_task_record')
                ->where('uid', '=', $uid)
                ->where('pid', '=', $pid)
                ->where('status', '=', 1)
                ->execute()
                ->as_array();
        $count_record = count($records);
        
        if($count_record == $count_total)
        {
             //更新到数据库
            $result = DB::update('huiyuan_prize_record')
                    ->set(array('status' => 1, 'progress' => 100, 'mtime' => date('Y-m-d H:i:s', time()) ))
                    ->where('uid', '=', $uid)
                    ->where('pid', '=', $pid)
                    ->execute();
         }else{
            //计算出进度
            $progress = (int)(($count_record/$count_total)*100);

            //更新到数据库
            $result = DB::update('huiyuan_prize_record')
                    ->set(array('progress' => $progress, 'mtime' => date('Y-m-d H:i:s', time()) ))
                    ->where('uid', '=', $uid)
                    ->where('pid', '=', $pid)
                    ->execute();
         }
    }
    
    public static function record($uid, $pid)
    {
        // 首先查询这个用户是否有参加这个福利的记录
        $query = DB::select()
                ->from('huiyuan_prize_record')
                ->where('uid', '=', $uid)
                ->where('pid', '=', $pid)
                ->execute()
                ->as_array();
        if(!empty($query))
        {
            return null;
        }
                  
        // 如果有则不做处理，如果没有则插入一条数据
        list($insert_id, $rows) = DB::insert('huiyuan_prize_record')
            ->columns (array('uid', 'pid', 'status', 'progress', 'ctime'))
            ->values (array($uid, $pid, 0, 0, date('Y-m-d H:i:s', time())))
            ->execute();
        
    }
    
}